package com.vhall.component.service.rbac.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.rbac.RbacUserRoleMapper;
import com.vhall.component.entity.rbac.UserRoleEntity;
import com.vhall.component.entity.rbac.vo.RoleVO;
import com.vhall.component.entity.rbac.vo.UserAddRoleReqVO;
import com.vhall.component.entity.rbac.vo.UserDeleteRoleReqVO;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.rbac.RbacRoleService;
import com.vhall.component.service.rbac.RbacUserRoleService;
import com.vhall.component.service.rbac.constant.AccessConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dalong
 * @date 2021-05-11 16:13
 */
@Service
public class RbacUserRoleServiceImpl implements RbacUserRoleService {

    @Autowired
    private RbacUserRoleMapper userRoleMapper;

    @Autowired
    private RbacRoleService roleService;

    @Override
    public UserRoleEntity getInfo(String accountId, String appId, Integer status) {
        LambdaQueryWrapper<UserRoleEntity> where = Wrappers.lambdaQuery();
        where.eq(UserRoleEntity::getAccountId, accountId)
                .eq(UserRoleEntity::getAppId, appId)
                .eq(UserRoleEntity::getStatus, status)
                .isNull(UserRoleEntity::getDeletedAt)
                .last("LIMIT 1");
        return userRoleMapper.selectOne(where);
    }


    /***
     * 看传的参数是多个用户。应该加事务
     * @param reqVO
     * @return
     */
    @Override
    public boolean batchCreate(UserAddRoleReqVO reqVO) {
        // 检查角色是否存在
        RoleVO roleVO = roleService.getInfoById(reqVO.getRoleId());
        if (roleVO == null) {
            throw new BusinessException(BizErrorCode.BIZ_ROLE_NOT_EXIST);
        }
        try {
            List<Integer> accountIdList = JSONObject.parseArray(reqVO.getAccountId(), Integer.class);
            for (Integer accountId : accountIdList) {
                LambdaQueryWrapper<UserRoleEntity> where = Wrappers.lambdaQuery();
                where.eq(UserRoleEntity::getRoleId, reqVO.getRoleId())
                        .eq(UserRoleEntity::getAccountId, accountId)
                        .eq(UserRoleEntity::getAppId, reqVO.getAppId())
                        .isNull(UserRoleEntity::getDeletedAt)
                        .last("LIMIT 1");
                UserRoleEntity userRoleEntity = userRoleMapper.selectOne(where);
                if (userRoleEntity != null) {
                    if (AccessConstant.DEL_STATUS.equals(userRoleEntity.getStatus())) {
                        userRoleEntity.setStatus(AccessConstant.NORMAL_STATUS);
                        userRoleEntity.setDeletedAt(null);
                        userRoleEntity.setUpdatedAt(LocalDateTime.now());
                        userRoleMapper.updateById(userRoleEntity);
                    }
                } else {
                    userRoleEntity = new UserRoleEntity();
                    userRoleEntity.setRoleId(reqVO.getRoleId());
                    userRoleEntity.setAppId(reqVO.getAppId());
                    userRoleEntity.setAccountId(accountId.toString());
                    userRoleMapper.insert(userRoleEntity);
                }
            }
        } catch (Exception e) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        return true;
    }

    /**
     * 应该返回Integer
     *
     * @param reqVO
     * @return
     */
    @Override
    public boolean batchDelete(UserDeleteRoleReqVO reqVO) {

        List<Integer> accountIdList;
        try {
            accountIdList = JSONObject.parseArray(reqVO.getAccountId(), Integer.class);
        } catch (Exception e) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        for (Integer integer : accountIdList) {
            LambdaQueryWrapper<UserRoleEntity> where = Wrappers.lambdaQuery();
            where.eq(UserRoleEntity::getRoleId, reqVO.getRoleId())
                    .eq(UserRoleEntity::getAccountId, integer)
                    .eq(UserRoleEntity::getAppId, reqVO.getAppId())
                    .isNull(UserRoleEntity::getDeletedAt)
                    .last("LIMIT 1");
            UserRoleEntity userRoleEntity = userRoleMapper.selectOne(where);
            if (userRoleEntity != null) {
                throw new BusinessException(BizErrorCode.BIZ_ROLE_ALREADY_BIND_NOT_DELETE);
            }
        }
        LambdaUpdateWrapper<UserRoleEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(UserRoleEntity::getRoleId, reqVO.getRoleId())
                .in(UserRoleEntity::getAccountId, accountIdList)
                .isNull(UserRoleEntity::getDeletedAt)
                .set(UserRoleEntity::getStatus, AccessConstant.DEL_STATUS)
                .set(UserRoleEntity::getDeletedAt, LocalDateTime.now());
        userRoleMapper.update(null, updateWrapper);
        return true;
    }
}
